package com.fare.fdc.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.analyse.AirCompanyTrendContrastCabinApplication;
import com.fare.taluo.application.analyse.AirCompanyTrendContrastFlightApplication;
import com.fare.taluo.application.analyse.AirCompanyTrendContrastLineApplication;
import com.fare.taluo.application.fetl.SinglePlaneInfoApplication;
import com.fare.taluo.application.plane.cabin.ICabinGradeApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.plane.cabin.CabinGradeVO;
import com.fare.taluo.vo.single.CabinVO;

@Controller("airCompanyTrendLineFlightCabinAction")
@Scope("prototype")
public class AirCompanyTrendLineFlightCabinAction extends BaseFdcAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5288448791945731209L;
	private String language;
	private String fromCity;
	private String fromCityCode;
	private String toCity;
	private String toCityCode;
	private String flightStartDate;
	private String inputArrCityCodes;
	private String inputDepCityCodes;
	private String flightEndDate;
	private Integer futureDay;//未来天数
	private String[] periods;//出发时段,AMPN
	private Integer selfStartPeriods;//自定义出发时段（开始）(小时)
	private Integer selfEndPeriods;//自定义出发时段（结束）(小时)
	//private String compareCompanyHbaseKey;//比较公司（我行）
	private String[] comparedCompanyHbaseKeys;//被比较的其他公司
	private String carrierFlightnoCabin;
	private String dataSourceHbaseKey;//数据来源
	private List<SourceEntityVO> crawlCompanys;//所有的当前处于抓取状态的公司
	private List<SourceEntityVO> crawlChannels;//所有的当前处于抓取状态的渠道
	private SourceEntityVO myCompanyOrChannel;//指当前用户所在的公司或者渠道
	private String priceType;//价格类型，如最低价，最高价
	@Resource(name = "airCompanyTrendContrastLineApplicationImpl")
	private AirCompanyTrendContrastLineApplication airCompanyTrendContrastLineApplication;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;
	
	private String flightnos;//航班对比所有航班号
	//private String[] compareFlights;// 比较航班
	private String companyCarrierKey;// 在获取航空公司下航线的所有航班的时候需要用到的公司key
	private String flightNo;
	private String carrierKey;
	@Resource(name = "airCompanyTrendContrastFlightApplicationImpl")
	private AirCompanyTrendContrastFlightApplication airCompanyTrendContrastFlightApplication;
	@Resource(name = "flightInfoApplication")
	private FlightInfoApplication flightInfoApplication;
	@Resource(name = "singlePlaneInfoApplication")
	private SinglePlaneInfoApplication singlePlaneInfoApplication;
	@Resource(name = "airCompanyTrendContrastCabinApplication")
	private AirCompanyTrendContrastCabinApplication airCompanyTrendContrastCabinApplication;
	@Inject
	private ICabinGradeApplication cabinGradeApplication;
	public void analyseLine()throws Exception{
			Locale locale = super.getLocale();
			String jsonStr=this.airCompanyTrendContrastLineApplication.getFusionChartsXMLStr(
					fromCity, toCity,fromCityCode, toCityCode, 
					flightStartDate, flightEndDate, futureDay, 
					periods, selfStartPeriods, selfEndPeriods, 
					companyCarrierKey, comparedCompanyHbaseKeys, dataSourceHbaseKey, priceType,locale);
			super.toJSONOut(jsonStr);
	}
	public void analyseFlight() throws Exception{
		Locale locale = super.getLocale();
		if(fromCityCode ==null || "".equals(fromCityCode)){
			if(inputArrCityCodes !=null && !"".equals(inputDepCityCodes)){
				fromCityCode=inputDepCityCodes.substring(inputDepCityCodes.indexOf("(")+1, inputDepCityCodes.lastIndexOf(")"));
			}
		}
		if(toCityCode ==null || "".equals(toCityCode)){
			if(inputArrCityCodes !=null && !"".equals(inputArrCityCodes)){
				toCityCode=inputArrCityCodes;
			}
		}
		String jsonStr = airCompanyTrendContrastFlightApplication.getFusionChartsXMLStr(fromCityCode, toCityCode, 
					flightStartDate, flightEndDate, futureDay, flightnos.split(","), dataSourceHbaseKey,priceType,locale);
		super.toJSONOut(jsonStr);
	}
	public void analyseCabin() throws Exception{
		
		/*String xml = airCompanyTrendContrastFlightApplication.getFusionChartsXMLStr(fromCityCode, toCityCode, 
				flightStartDate, flightEndDate, futureDay, carrierFlightnoCabin.split(","), dataSourceHbaseKey,priceType);*/
		Locale locale = super.getLocale();
		if(fromCityCode ==null || "".equals(fromCityCode)){
			if(inputArrCityCodes !=null && !"".equals(inputDepCityCodes)){
				fromCityCode=inputDepCityCodes.substring(inputDepCityCodes.indexOf("(")+1, inputDepCityCodes.lastIndexOf(")"));
			}
		}
		if(toCityCode ==null || "".equals(toCityCode)){
			if(inputArrCityCodes !=null && !"".equals(inputArrCityCodes)){
				toCityCode=inputArrCityCodes;
			}
		}
		String xml = airCompanyTrendContrastCabinApplication.getFusionChartsXMLStr(fromCityCode, toCityCode, flightStartDate, flightEndDate, carrierFlightnoCabin.split(","), dataSourceHbaseKey, priceType,locale);
		super.toJSONOut(xml);
	}
	
	public void getCabinsByConditions() throws Exception{
		SourceEntityVO source = null;
		//如果抓取渠道为官网，则找出该官网hbase key
		if(com.fare.util.StringUtils.isEmpty(dataSourceHbaseKey)){
			source = sourceApplication.findByCarrierKey(carrierKey).get(0);
		}else{
			source = sourceApplication.findByHbaseKey(dataSourceHbaseKey);
		}
		Locale locale = super.getLocale();
		source.setName(null==I18nUtil.getProperties(locale).get(source.getName())?source.getName():I18nUtil.getProperties(locale).get(source.getName()));
		source.setShortName(null==I18nUtil.getProperties(locale).get(source.getShortName())?source.getShortName():I18nUtil.getProperties(locale).get(source.getShortName()));
		StringBuffer resultBuffer = null;
		List<CabinVO> list = null;
		List<CabinGradeVO> cabinGradeList;
		try {
			cabinGradeList = cabinGradeApplication.list(source.getId());
			list = convertGradeList2CabinList(cabinGradeList, source);
			//list =  singlePlaneInfoApplication.findPlaneInfoByCondition(grabChannel,carrierKey, fromCityCode, toCityCode, null, flightNo);
			//国际化
			if(list.size()>0){
				I18nUtil.cabinI18N(list, locale);
			 }
			 resultBuffer = new StringBuffer();
			resultBuffer.append("{\"Total\":"+list.size()+","+ "\"Rows\":"+com.fare.taluo.util.JsonUtil.toJson(list)+"}");	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		super.toJSONOut(resultBuffer.toString());	
		//System.out.println(list.size());
	}
	private List<CabinVO> convertGradeList2CabinList(List<CabinGradeVO> cabinGradeList,SourceEntityVO source){
		List<CabinVO> returnList = new ArrayList<CabinVO>();
		Locale locale = super.getLocale();
		if(StringUtils.checkList(cabinGradeList)){
			for (CabinGradeVO cabinGradeVO : cabinGradeList) {
				CabinVO vo =  new CabinVO();
				vo.setCabinType(cabinGradeVO.getName());
				cabinGradeVO.setName(null==I18nUtil.getProperties(locale).get(cabinGradeVO.getName())?vo.getCarrierFlightnoCabin():I18nUtil.getProperties(locale).get(cabinGradeVO.getName()));
				vo.setCarrierFlightnoCabin(carrierKey+"_"+flightNo+"_"+cabinGradeVO.getName()+"_"+source.getShortName()+"_"+source.getHbaseKey()+"_"+source.getShortName());
				returnList.add(vo);	
			}	
		}
		return returnList;
	}
	
	//private List
	
	public String toAnalyseJSP() throws Exception {
		this.crawlCompanys=sourceApplication.findCrawlAirCompanys();
		this.crawlChannels=sourceApplication.findCrawlChannel();
		Locale locale = super.getLocale();
		I18nUtil.sourceConvert2I18N(crawlCompanys, locale);
		I18nUtil.sourceConvert2I18N(crawlChannels, locale);
		request.setAttribute("maxDate", DateUtils.getDateAfter(29));//目前只支持30天的数据 
		returnPageURL = "/fare/fdc/_lineFlightCabin.jsp";
		language=locale.toString();
		if(!language.equalsIgnoreCase("zh_CN") && !language.equalsIgnoreCase("en_US")){
			language="other";
		}
		return "dispatcher";
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getFromCityCode() {
		return fromCityCode;
	}

	public void setFromCityCode(String fromCityCode) {
		this.fromCityCode = fromCityCode;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public String getToCityCode() {
		return toCityCode;
	}

	public void setToCityCode(String toCityCode) {
		this.toCityCode = toCityCode;
	}

	public String getFlightStartDate() {
		return flightStartDate;
	}

	public void setFlightStartDate(String flightStartDate) {
		this.flightStartDate = flightStartDate;
	}

	public String getFlightEndDate() {
		return flightEndDate;
	}

	public void setFlightEndDate(String flightEndDate) {
		this.flightEndDate = flightEndDate;
	}

	public Integer getFutureDay() {
		return futureDay;
	}

	public void setFutureDay(Integer futureDay) {
		this.futureDay = futureDay;
	}

	public String[] getPeriods() {
		return periods;
	}

	public void setPeriods(String[] periods) {
		this.periods = periods;
	}

	public Integer getSelfStartPeriods() {
		return selfStartPeriods;
	}

	public void setSelfStartPeriods(Integer selfStartPeriods) {
		this.selfStartPeriods = selfStartPeriods;
	}

	public Integer getSelfEndPeriods() {
		return selfEndPeriods;
	}

	public void setSelfEndPeriods(Integer selfEndPeriods) {
		this.selfEndPeriods = selfEndPeriods;
	}

	/*public String getCompareCompanyHbaseKey() {
		return compareCompanyHbaseKey;
	}

	public void setCompareCompanyHbaseKey(String compareCompanyHbaseKey) {
		this.compareCompanyHbaseKey = compareCompanyHbaseKey;
	}*/

	public String[] getComparedCompanyHbaseKeys() {
		return comparedCompanyHbaseKeys;
	}

	public void setComparedCompanyHbaseKeys(String[] comparedCompanyHbaseKeys) {
		this.comparedCompanyHbaseKeys = comparedCompanyHbaseKeys;
	}

	public String getDataSourceHbaseKey() {
		return dataSourceHbaseKey;
	}

	public void setDataSourceHbaseKey(String dataSourceHbaseKey) {
		this.dataSourceHbaseKey = dataSourceHbaseKey;
	}

	public List<SourceEntityVO> getCrawlCompanys() {
		return crawlCompanys;
	}

	public void setCrawlCompanys(List<SourceEntityVO> crawlCompanys) {
		this.crawlCompanys = crawlCompanys;
	}

	public List<SourceEntityVO> getCrawlChannels() {
		return crawlChannels;
	}

	public void setCrawlChannels(List<SourceEntityVO> crawlChannels) {
		this.crawlChannels = crawlChannels;
	}

	public SourceEntityVO getMyCompanyOrChannel() {
		return myCompanyOrChannel;
	}

	public void setMyCompanyOrChannel(SourceEntityVO myCompanyOrChannel) {
		this.myCompanyOrChannel = myCompanyOrChannel;
	}

/*	public String[] getCompareFlights() {
		return compareFlights;
	}

	public void setCompareFlights(String[] compareFlights) {
		this.compareFlights = compareFlights;
	}*/

	public String getCompanyCarrierKey() {
		return companyCarrierKey;
	}

	public void setCompanyCarrierKey(String companyCarrierKey) {
		this.companyCarrierKey = companyCarrierKey;
	}

	public FlightInfoApplication getFlightInfoApplication() {
		return flightInfoApplication;
	}

	public void setFlightInfoApplication(FlightInfoApplication flightInfoApplication) {
		this.flightInfoApplication = flightInfoApplication;
	}
	public String getPriceType() {
		return priceType;
	}
	public void setPriceType(String priceType) {
		this.priceType = priceType;
	}
	public String getFlightnos() {
		return flightnos;
	}
	public void setFlightnos(String flightnos) {
		this.flightnos = flightnos;
	}
	public String getFlightNo() {
		return flightNo;
	}
	public void setFlightNo(String flightNo) {
		this.flightNo = flightNo;
	}
	public String getCarrierKey() {
		return carrierKey;
	}
	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}
	public String getCarrierFlightnoCabin() {
		return carrierFlightnoCabin;
	}
	public void setCarrierFlightnoCabin(String carrierFlightnoCabin) {
		this.carrierFlightnoCabin = carrierFlightnoCabin;
	}
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public String getInputArrCityCodes() {
		return inputArrCityCodes;
	}
	public void setInputArrCityCodes(String inputArrCityCodes) {
		this.inputArrCityCodes = inputArrCityCodes;
	}
	public String getInputDepCityCodes() {
		return inputDepCityCodes;
	}
	public void setInputDepCityCodes(String inputDepCityCodes) {
		this.inputDepCityCodes = inputDepCityCodes;
	}
	
	
}
